package com.oracle.cmp.service;


import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.PageInfo;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.Customer;
import com.oracle.jdbc.util.Dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class CustomerServiceImpl implements CustomerService{

	
	/**
	 * 新增客户
	 * @param customer
	 * @return
	 */

	public Result save(Customer customer) {
		//向客户表中增加一条记录；	
		Dao.executeSql("insert into sys_customer values(default,?,?,?,?,?)",
				customer.getName(),customer.getTel(),customer.getCarno(),customer.getCartype(),customer.getCreatedate());
		return new Result(true,null,"保存成功");
	}

	
	/**
	 * 按客户名查询客户
	 * @return
	 */
	public Result queryForPage(Map<String,Object> map, int pageNum){
		String sql = "select * from sys_customer where 1=1";
		List params = new ArrayList();
		if(map.get("customerName")!=null&&!"".equals(map.get("customerName"))){
			sql += " and name = ?";
			params.add(map.get("customerName"));
		}
		sql+=" limit ?,?";

		params.add((pageNum-1)*Constant.PAGESIZE);
		params.add(Constant.PAGESIZE);
		List<Customer> list = Dao.query(sql,Customer.class,params.toArray());
		return new Result(true,list,"查询成功");

	}
	/**
	 * 按id查询客户
	 */
	public Result selectById(int customerId) {
		Customer customer = Dao.queryOne("select * from sys_customer where id = ?",Customer.class,customerId);
		return new Result(true,customer,"查询成功");
	}
	
	public Result update(Customer customer) {
		Dao.executeSql("update sys_customer set name = ?,tel=?,carno=?,cartype=?,createdate=? where id=?",
				customer.getName(),customer.getTel(),customer.getCarno(),customer.getCartype(),customer.getCreatedate(),customer.getId());
		return new Result(true,null,"修改成功");
	}
	
	public Result delete(int customerId) {
		Dao.executeSql("delete from sys_customer where id = ?",customerId);
		return new Result(true,null,"删除成功");
	}
	public int count(Map<String,Object> map){
		String sql = "select count(0) from sys_customer where 1=1";
		List params = new ArrayList();
		if(map.get("customerName")!=null&&!"".equals(map.get("customerName"))){
			sql += " and name = ?";
			params.add(map.get("customerName"));
		}
		int total = ((Long)Dao.queryOne(sql,params.toArray())[0]).intValue();
		return total;
	}
}
